#!/bin/zsh

# For all script types, returning an exit code of 0 (success) means the
# script execution completed successfully.
#
# Requirements scripts can have the following exit codes that
# influence how the Client will handle the Fileset:
#
# - 210: This exit code will cause the Fileset to be treated like the
#        installation was successful (unless another requirements script fails,)
#        but the Fileset will not be downloaded nor installed.
#
# - 220: This exit code will prevent the installation and cause the client to
#        stop retrying unless a manual action is made (verify, reinstall, etc.)
#        or the Fileset is updated.
#
# Returning any other exit code but 0 (e.g. 1 or -1) will be reported as a
# "Requirements Failure: Script" in the Client Info window and Fileset Report.
# This will also prevent the contents of the Fileset from downloading and
# installing. In this case, requirements scripts will be executed every 2
# minutes and the Fileset will be installed when they all return 0.
#
# For other types of scripts, any non-zero exit code (e.g. 1 or -1) will cause the
# Fileset installation to fail and a script failure to be reported.
#
# If the script finishes without returning an exit code, the exit code 0
# (success) is assumed by default.
#
# Add the contents of your script below:

pkgfile="GoogleChrome.pkg"
logfile="/Library/Logs/GoogleChromeInstallScript.log"
url='https://dl.google.com/chrome/mac/stable/gcem/GoogleChrome.pkg'

/bin/echo "--" >> ${logfile}
/bin/echo "`date`: Downloading latest version." >> ${logfile}
mkdir /private/tmp/chrome_install/
/usr/bin/curl -s -o /private/tmp/chrome_install/${pkgfile} ${url}

/bin/echo "`date`: Installing..." >> ${logfile}
/usr/sbin/installer -pkg /private/tmp/chrome_install/GoogleChrome.pkg -target /

/bin/sleep 5

/bin/echo "`date`: Deleting package installer." >> ${logfile}
/bin/rm -rf /private/tmp/chrome_install

exit 0

